﻿using System;
using ScrumPilot.Domain.Models;

namespace ScrumPilot.Domain.BPL
{
    public class ConfigServices : BusinessProcessLayer
    {
        public Main GetConfig()
        {
            using (var unitOfWork = new UnitOfWork())
            {
                Main config = null;
                tfsserverparam tp = unitOfWork.TfsServerParamsRepository.GetOne();
                scrumpilotparam spp = unitOfWork.ScrumPilotParamRepository.GetOne();
                if (tp != null)
                    config = new Main
                    {
                        TFSUrl = tp.tfsurl,
                        TFSPort = tp.tfsport,
                        TFSSubDir = tp.tfsdirectory,
                        TFSID = tp.tfsuser,
                        TFSPWD = tp.tfspassword,
                        ScrumPilotWSAdress = tp.spwsaddress,
                        DashBoardType = spp.dashboardtype ?? 0,
                        BugNormalLevel = spp.buglevelok ?? 5,
                        BugOrangeLevel = spp.buglevelorange ?? 10,
                        BugRedLevel = spp.buglevelred ?? 20,
                        TestToDoNormalLevel = spp.testtodolevelok ?? 5,
                        TestToDoOrangeLevel = spp.testtodolevelorange ?? 10,
                        TestToDoRedLevel = spp.testtodolevelred ?? 20,
                        KanbanMaxDoingLevel = spp.kanbanmaxdoing ?? 0,
                        KanbanMaxToTest = spp.kanbanmaxtotest ?? 0,
                        YammerConsumerKey = spp.yammerconsumer_key,
                        YammerConsumerSecret = spp.yammerconsumer_secret,
                        YammerGroup = spp.yammergroup ?? -1,
                        YammerTopic = spp.yammertopic
                    };
                return config;
            }
        }

        public Byte GetDashBoardType()
        {
            using (var unitOfWork = new UnitOfWork())
            {
                scrumpilotparam spp = unitOfWork.ScrumPilotParamRepository.GetOne();
                return spp.dashboardtype ?? 0;
            }
        }

        public Boolean SaveConfig(Main config)
        {
            using (var unitOfWork = new UnitOfWork())
            {
                tfsserverparam tfsparams = unitOfWork.TfsServerParamsRepository.GetOne();
                scrumpilotparam spParams = unitOfWork.ScrumPilotParamRepository.GetOne();

                if (tfsparams == null) // a new one
                    unitOfWork.TfsServerParamsRepository.Add(new tfsserverparam
                        {
                            tfsurl = config.TFSUrl,
                            tfsport = config.TFSPort,
                            tfsdirectory = config.TFSSubDir,
                            tfsuser = config.TFSID,
                            tfspassword = config.TFSPWD,
                            spwsaddress = config.ScrumPilotWSAdress
                        });
                else
                {
                    tfsparams.tfsurl = config.TFSUrl;
                    tfsparams.tfsport = config.TFSPort;
                    tfsparams.tfsdirectory = config.TFSSubDir;
                    tfsparams.tfsuser = config.TFSID;
                    tfsparams.tfspassword = config.TFSPWD;
                    tfsparams.spwsaddress = config.ScrumPilotWSAdress;
                }
                if (spParams == null)
                {
                    unitOfWork.ScrumPilotParamRepository.Add(new scrumpilotparam
                        {
                            buglevelok = config.BugNormalLevel,
                            buglevelorange = config.BugOrangeLevel,
                            buglevelred = config.BugRedLevel,
                            testtodolevelok = config.TestToDoNormalLevel,
                            testtodolevelorange = config.TestToDoOrangeLevel,
                            testtodolevelred = config.TestToDoRedLevel,
                            kanbanmaxdoing = config.KanbanMaxDoingLevel,
                            kanbanmaxtotest = config.KanbanMaxToTest,
                            yammerconsumer_key = config.YammerConsumerKey,
                            yammerconsumer_secret = config.YammerConsumerSecret,
                            yammergroup = config.YammerGroup,
                            yammertopic = config.YammerTopic,
                            dashboardtype = config.DashBoardType
                        });
                }
                else
                {
                    spParams.buglevelok = config.BugNormalLevel;
                    spParams.buglevelorange = config.BugOrangeLevel;
                    spParams.buglevelred = config.BugRedLevel;
                    spParams.testtodolevelok = config.TestToDoNormalLevel;
                    spParams.testtodolevelorange = config.TestToDoOrangeLevel;
                    spParams.testtodolevelred = config.TestToDoRedLevel;
                    spParams.kanbanmaxdoing = config.KanbanMaxDoingLevel;
                    spParams.kanbanmaxtotest = config.KanbanMaxToTest;
                    spParams.yammerconsumer_key = config.YammerConsumerKey;
                    spParams.yammerconsumer_secret = config.YammerConsumerSecret;
                    spParams.yammergroup = config.YammerGroup;
                    spParams.yammertopic = config.YammerTopic;
                    spParams.dashboardtype = config.DashBoardType;
                }
                return unitOfWork.Save() > 0;
            }
        }

    }
}
